package org.edu.modules.sxjy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.entity.VeCompanyUser;
import org.edu.modules.sxjy.entity.VePost;
import org.edu.modules.sxjy.mapper.VePostMapper;
import org.edu.modules.sxjy.service.IVePostService;
import org.edu.modules.sxjy.vo.VePostUpdateVo;
import org.edu.modules.sxjy.vo.VePostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VePostServiceImpl
        extends ServiceImpl<VePostMapper, VePost>
        implements IVePostService
{
    @Autowired
    private VePostMapper vePostMapper;
    @Autowired
    private IVePostService vePostService;

    public List<VePostVo> exportXls(VeCommon veCommon)
    {
        List<VePostVo> vePostVos = new ArrayList();
        VeCompanyUser veCompanyUser;
        if ((veCommon.getUserType().contains("3")) || (veCommon.getUserType().contains("4")))
        {
            veCompanyUser = this.vePostMapper.getCpByUserId(veCommon.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                veCommon.setCpId(veCompanyUser.getCpId());
                vePostVos = this.vePostMapper.exportXls(veCommon);
            }
            else if ((veCompanyUser == null) || ("".equals(veCompanyUser)))
            {
                vePostVos = new ArrayList();
            }
        }
        else
        {
            vePostVos = this.vePostMapper.exportXls(veCommon);
        }
        if ((vePostVos != null) && (vePostVos.size() > 0)) {
            for (VePostVo postVo : vePostVos) {
                if ((postVo.getCteacherUserId() != 0) &&
                        (this.vePostMapper.getCpUserNameByCpUserId(postVo.getCteacherUserId()) != null)) {
                    postVo.setRealName(this.vePostMapper.getCpUserNameByCpUserId(postVo.getCteacherUserId()));
                }
            }
        }
        return vePostVos;
    }

    public List<VePostVo> selectAll(VePostVo vePostVo, Integer pageNo, Integer pageSize, VeCommon veCommon)
    {
        List<VePostVo> vePostVos = new ArrayList();
        VeCompanyUser veCompanyUser;
        if ((veCommon.getUserType().contains("3")) || (veCommon.getUserType().contains("4")))
        {
            veCompanyUser = this.vePostMapper.getCpByUserId(veCommon.getUserId());
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser)))
            {
                veCommon.setCpId(veCompanyUser.getCpId());
                vePostVos = this.vePostMapper.selectAll(vePostVo, pageNo, pageSize, veCommon);
            }
            else if ((veCompanyUser == null) || ("".equals(veCompanyUser)))
            {
                vePostVos = new ArrayList();
            }
        }
        else if (veCommon.getUserType().equals("2"))
        {
            veCommon.setIsCpFiling("1");
            vePostVos = this.vePostMapper.selectAll(vePostVo, pageNo, pageSize, veCommon);
        }
        else
        {
            vePostVos = this.vePostMapper.selectAll(vePostVo, pageNo, pageSize, veCommon);
        }
        if ((vePostVos != null) && (vePostVos.size() > 0)) {
            for (VePostVo postVo : vePostVos)
            {
                String realNameById = this.vePostMapper.getRealNameById(((VePost)this.vePostService.getById(Integer.valueOf(postVo.getId()))).getCteacherUserId());
                if ((realNameById != null) && (!"".equals(realNameById))) {
                    postVo.setRealName(realNameById);
                }
            }
        }
        return vePostVos;
    }

    public int getFirmNameId(String name)
    {
        return this.vePostMapper.getFirmNameId(name);
    }

    public List<Integer> getTeacharNameId(String realName, int cpId)
    {
        List<Integer> teacharNameId = this.vePostMapper.getTeacharNameId(realName, cpId);
        if ((teacharNameId != null) && (!"".equals(teacharNameId))) {
            return teacharNameId;
        }
        return null;
    }

    public List<VeCompanyUser> getTeacharIdByCpId(int cpId)
    {
        List<VeCompanyUser> teacharNameId = this.vePostMapper.getTeacharIdByCpId(cpId);
        if ((teacharNameId != null) && (!"".equals(teacharNameId))) {
            return teacharNameId;
        }
        return null;
    }

    public List<VePostVo> getSumPage(VePostVo vePostVo, VeCommon veCommon)
    {
        return this.vePostMapper.getSumPage(vePostVo, veCommon);
    }

    public String getPostNameById(int postId)
    {
        return this.vePostMapper.getPostNameById(postId);
    }

    public void edit(VePostUpdateVo vePostUpdateVo)
    {
        VePost vePost = (VePost)this.vePostService.getById(Integer.valueOf(vePostUpdateVo.getId()));


        vePost.setStatus(vePostUpdateVo.getStatus());


        vePost.setPostName(vePostUpdateVo.getPostName());


        vePost.setCteacherUserId(vePostUpdateVo.getCteacherUserId());



        vePost.setCpId(vePostUpdateVo.getCpId());


        this.vePostService.updateById(vePost);
    }

    public VePostUpdateVo editEcho(int id)
    {
        VePost vePost = (VePost)this.vePostService.getById(Integer.valueOf(id));

        VePostUpdateVo vePostUpdateVo = new VePostUpdateVo();


        vePostUpdateVo.setStatus(vePost.getStatus());


        vePostUpdateVo.setPostName(vePost.getPostName());


        vePostUpdateVo.setCteacherUserId(vePost.getCteacherUserId());


        vePostUpdateVo.setCpId(vePost.getCpId());


        return vePostUpdateVo;
    }

    public Result<?> getPostNameByCpId(int id)
    {
        if (id != 0)
        {
            List<VePost> vePostList = this.vePostMapper.getPostNameByCpId(id);
            return Result.OK(vePostList);
        }
        return Result.error("获取失败，企业信息不存在");
    }

    public boolean importExcel(VePostVo vePostVo)
    {
        VePost vePost = new VePost();

        int firmNameId = this.vePostMapper.getFirmNameId(vePostVo.getName());
        if (firmNameId == 0) {
            return false;
        }
        vePost.setCpId(firmNameId);

        vePost.setPostName(vePostVo.getPostName());

        List<Integer> teacharNameId = this.vePostService.getTeacharNameId(vePostVo.getRealName(), firmNameId);
        if (teacharNameId.size() == 0) {
            return false;
        }
        vePost.setCteacherUserId(((Integer)teacharNameId.get(0)).intValue());









        vePost.setStatus(1);
        vePost.setCreateTime(System.currentTimeMillis());
        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        vePost.setCreateUserId(loginUser.getId());
        vePost.setTerminalId(1);

        this.vePostService.save(vePost);

        return true;
    }

    public void add(VePostVo vePostVo)
    {
        VePost vePost = new VePost();



        vePost.setCpId(vePostVo.getCpId());

        vePost.setPostName(vePostVo.getPostName());

        vePost.setCteacherUserId(vePostVo.getCteacherUserId());

        vePost.setStatus(vePostVo.getStatus());

        vePost.setCreateTime(System.currentTimeMillis());

        LoginUser loginUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        vePost.setCreateUserId(loginUser.getId());

        vePost.setTerminalId(1);

        this.vePostService.save(vePost);
    }

    public Result<?> getPostCpIdByUserId(String userType, String userId)
    {
        if (userType.equals("3"))
        {
            VeCompanyUser veCompanyUser = this.vePostMapper.getPostCpIdByUserId(userId);
            if ((veCompanyUser != null) && (!"".equals(veCompanyUser))) {
                return Result.OK(veCompanyUser);
            }
        }
        return Result.OK();
    }
}
